﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmCanBo : Form
    {        
        public frmCanBo()
        {
            InitializeComponent();
        }        
        void Reload()
        {
            ReloadList();
        }
        void ReloadList()
        {            
            lstDanhSachCanBo.DisplayMember = "Ten";
            lstDanhSachCanBo.ValueMember = "ID";
            if (rdbTatCa.Checked)
            {
                List<NguoiDTO> tmp = NguoiBUS.LoadAll();
                tmp.ForEach(GolbalSettings.MarkName);
                lstDanhSachCanBo.DataSource = tmp;
            }
            else if (rdbCanBo.Checked)
            {
                lstDanhSachCanBo.DataSource = NguoiBUS.LoadCurrentOnly();
            }
            else if (rdbCuuCanBo.Checked)
            {
                lstDanhSachCanBo.DataSource = NguoiBUS.LoadExOnly();
            }
            lstDanhSachCanBo.SelectedIndex = -1;
        }
        private void frmCanBo_Load(object sender, EventArgs e)
        {
            btnLuu.Enabled = false;
            btnThem.Enabled = false;
            chkCuuCanBo.Checked = false;
            chkCuuCanBo.Enabled = false;
            chkSuaCuuCanBo.Checked = false;
            chkSuaCuuCanBo.Enabled = false;
            Reload();            

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void rdb_CheckedChanged(object sender, EventArgs e)
        {
            Reload();
        }

        private void lstDanhSachCanBo_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            if (lstDanhSachCanBo.SelectedIndex == -1)
            {
                txtTenCanBo.Text = "";
                txtTenCanBo.Enabled = false;
                chkSuaCuuCanBo.Checked = false;
                
                return;                
            }
            
            NguoiDTO tmp = ((NguoiDTO)lstDanhSachCanBo.SelectedItem);

            txtTenCanBo.Text = tmp.Ten;
            txtTenCanBo.Enabled = true;

            if (tmp.TinhTrang)
            {
                chkSuaCuuCanBo.Checked = true;
                if (rdbTatCa.Checked)
                {
                    txtTenCanBo.Text = GolbalSettings.RemoveMark(txtTenCanBo.Text);
                }
            }
            else
            {
                chkSuaCuuCanBo.Checked = false;
            }
        }

        private void txtTenCanBo_TextChanged(object sender, EventArgs e)
        {
            if (txtTenCanBo.Text.Trim() == "" || lstDanhSachCanBo.SelectedIndex==-1)
            {                
                chkSuaCuuCanBo.Enabled = false;
                btnLuu.Enabled = false;
            }
            else
            {
                chkSuaCuuCanBo.Enabled = true;
                btnLuu.Enabled = true;
            }
        }

        private void txtCanBoMoi_TextChanged(object sender, EventArgs e)
        {
            if (txtCanBoMoi.Text.Trim() == "")
            {
                chkCuuCanBo.Enabled = false;
                btnThem.Enabled = false;
            }
            else
            {
                chkCuuCanBo.Enabled = true;
                btnThem.Enabled = true;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            NguoiDTO canBoMoi = new NguoiDTO(0, txtCanBoMoi.Text.Trim(), chkCuuCanBo.Checked);
            try
            {
                if (NguoiBUS.Add(canBoMoi))
                {
                    if ((canBoMoi.TinhTrang && !rdbCanBo.Checked) ||
                        (!canBoMoi.TinhTrang && !rdbCuuCanBo.Checked))
                    {
                        Reload();
                    }
                    txtCanBoMoi.Text = "";
                    chkCuuCanBo.Checked = false;
                }
                else
                {
                    MessageBox.Show("Không thể thêm cán bộ mới.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            NguoiDTO canBo = new NguoiDTO((int)lstDanhSachCanBo.SelectedValue, txtTenCanBo.Text.Trim(), chkSuaCuuCanBo.Checked);
            try
            {
                if (NguoiBUS.Update(canBo))
                {
                    Reload();
                    txtTenCanBo.Text = "";
                    chkSuaCuuCanBo.Checked = false;
                }
                else
                {
                    MessageBox.Show("Không thể thay đổi thông tin cán bộ.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

    }
}
